package com.jzd.fwserver.util

import io.netty.buffer.Unpooled
import java.io.PrintWriter
import java.io.StringWriter
import java.util.*


/**
 * @return i1 string to bytes
 */
fun String.toByteArrayByASCII(): ByteArray
{
    return this.toByteArray(Charsets.US_ASCII)
}

/**
 * @return bytes to i1 string
 */
fun ByteArray.toASCII(): String
{
    return this.toString(Charsets.US_ASCII).trimEnd(0.toChar())
}

/**
 * @return convert byte array to hex string
 */
fun ByteArray.toHexString(): String
{
    val sb = StringBuilder()
    if (this.count() > 100)
    {
        for (i in 0..97)
        {
            sb.append("%02x ".format(this[i]))
        }
        sb.append(".. %02x ".format(this[this.count() - 3]))
        sb.append("%02x ".format(this[this.count() - 2]))
        sb.append("%02x ".format(this[this.count() - 1]))
    }
    else
    {
        for (i in this)
        {
            sb.append("%02x ".format(i))
        }
    }
    return sb.toString()
}

/**
 * @return calculate crc16 for byte array
 */
fun ByteArray.crc16(): ByteArray
{
    return I2Helper.crc16(this)
}

/**
 * @param hi
 * @param lo
 * @return
 */
fun intOf(hi: Byte, lo: Byte): Int
{
    return (hi.toInt() and 0xFF shl 8) or (lo.toInt() and 0xFF)
}

fun intOf(b4: Byte, b3: Byte, b2: Byte, b1: Byte): Int
{
    return (b4.toInt() and 0xFF shl 24) or (b3.toInt() and 0xFF shl 16) or (b2.toInt() and 0xFF shl 8) or (b1.toInt() and 0xFF)
}

/**
 * @return timestamp for i1 format
 */
fun timestamp(): ByteArray
{
    val timestamp = System.currentTimeMillis() / 1000 + 28800
    return byteArrayOf(
            (timestamp and 0xFF).toByte(),
            (timestamp shr 8 and 0xFF).toByte(),
            (timestamp shr 16 and 0xFF).toByte(),
            (timestamp shr 24 and 0xFF).toByte()
    )
}

/**
 * beijing date to timestamp
 */
fun Date.toBJTimestamp(): ByteArray
{
    val timestamp = time / 1000
    return byteArrayOf(
            (timestamp and 0xFF).toByte(),
            (timestamp shr 8 and 0xFF).toByte(),
            (timestamp shr 16 and 0xFF).toByte(),
            (timestamp shr 24 and 0xFF).toByte()
    )
}

/**
 * date to timestamp
 */
fun Date.toTimestamp(): ByteArray
{
    val timestamp = time / 1000 + 28800
    return byteArrayOf(
            (timestamp and 0xFF).toByte(),
            (timestamp shr 8 and 0xFF).toByte(),
            (timestamp shr 16 and 0xFF).toByte(),
            (timestamp shr 24 and 0xFF).toByte()
    )
}

/**
 * convert 4lens byte array to long
 */
fun ByteArray.toTimestamp(): Long
{
    if (this.count() != 4)
        return System.currentTimeMillis()
    return Unpooled.copiedBuffer(this).readUnsignedIntLE() - 28800
}


/**
 * run command
 */
fun String.execute(): Process = Runtime.getRuntime().exec(this)

/**
 * convert Int to 4lens byte array
 */
fun Int.toArray(): ByteArray
{
    return byteArrayOf(
            (this and 0xFF).toByte(),
            (this shr 8 and 0xFF).toByte(),
            (this shr 16 and 0xFF).toByte(),
            (this shr 24 and 0xFF).toByte()
    )
}

/**
 * convert Long to 4lens byte array
 */
fun Long.toArray(): ByteArray
{
    return byteArrayOf(
            (this and 0xFF).toByte(),
            (this shr 8 and 0xFF).toByte(),
            (this shr 16 and 0xFF).toByte(),
            (this shr 24 and 0xFF).toByte()
    )
}

//获取异常堆栈信息
fun Throwable.getStackTraceString(): String?
{
    val sw = StringWriter()
    val pw = PrintWriter(sw)
    return pw.use { pw ->
        this.printStackTrace(pw)
        sw.toString()
    }
}

